-----------------------------------------------------------------------------------------
--
-- tutorial.lua
--
-----------------------------------------------------------------------------------------

local storyboard = require( "storyboard" )
local scene = storyboard.newScene()

-- include Corona's "widget" library
local widget = require "widget"
local gameConstant = require "GameConstant";
--------------------------------------------
local soundLeft
local soundRight
local graveLeft
local graveRight
local background
local titleLogo
local backBtn
local timeMoveButton = 200
local timeMoveToButton = 200
local i = 0
local statusSound = false
local statusGrave = false
local soundText
local soundGrave
local audioGame = require("audioGame");
-- forward declarations and other locals
local listenerBackBtn = function( obj )
            transition.to( backBtn, { time = timeMoveToButton, alpha=1.0,x = display.contentWidth*0.5, y=5*display.contentHeight/6} )
        end

-- 'onRelease' event listener for playBtn
local function onBackBtnRelease( )
	backBtn.xScale = 1;
        backBtn.yScale = 1;
        storyboard.removeScene( "tutorial" );
        local menu = storyboard.loadScene( "menu" )
        -- Later, transition to the scene (no loading necessary)
        if soundLeft then soundLeft:removeSelf( ); soundLeft = nil end 
        if soundRight then soundRight:removeSelf( ); soundRight = nil end 
        if graveLeft then graveLeft:removeSelf( ); graveLeft = nil end 
        if graveRight then graveRight:removeSelf( ); graveRight = nil end 
        storyboard.gotoScene( "menu", "fade", 100 )
	return true	-- indicates successful touch
    end
-- "OnPress" event listener for backBtn
local function onBackBtnPress()
    if gameConstant.statusSound then 
        audio.play( audioGame.buttonClick)
    end
    backBtn.xScale = 1.5;backBtn.yScale = 1.5;
    return true;
end 

-- forward declarations and other locals
local listenerSoundLeft = function( obj )
            transition.to( soundLeft, { time = timeMoveToButton, alpha=1.0,x = display.contentWidth/3-20, y=display.contentHeight/3} )
        end

-- 'onRelease' event listener for soundLeft
local function soundLeftRelease( )
	soundLeft.xScale = 1;
        soundLeft.yScale = 1;
        if statusSound then statusSound = false
        else statusSound = true end
        
	return true	-- indicates successful touch
    end
-- "OnPress" event listener for soundLeft
local function soundLeftPress()
    if gameConstant.statusSound then 
        audio.play( audioGame.buttonClick)
    end
    soundLeft.xScale = 1.5;soundLeft.yScale = 1.5;
    return true;
end  


-- forward declarations and other locals
local listenerSoundRight = function( obj )
            transition.to( soundRight, { time = timeMoveToButton, alpha=1.0,x = 2*display.contentWidth/3+20, y=display.contentHeight/3} )
        end

-- 'onRelease' event listener for soundLeft
local function soundRightRelease( )
	soundRight.xScale = 1;
        soundRight.yScale = 1;
        if statusSound then statusSound = false
        else statusSound = true end
        
	return true	-- indicates successful touch
    end
-- "OnPress" event listener for backBtn
local function soundRightPress()
    if gameConstant.statusSound then 
        audio.play( audioGame.buttonClick)
    end
    soundRight.xScale = 1.5;soundRight.yScale = 1.5;
    return true;
end 


-- forward declarations and other locals
local listenerGraveLeft = function( obj )
            transition.to( graveLeft, { time = timeMoveToButton, alpha=1.0,x = display.contentWidth/3-20, y=display.contentHeight/3+50} )
        end

-- 'onRelease' event listener for soundLeft
local function graveLeftRelease( )
	graveLeft.xScale = 1;
        graveLeft.yScale = 1;
        if statusGrave then statusGrave = false
        else statusGrave = true end
        
	return true	-- indicates successful touch
    end
-- "OnPress" event listener for backBtn
local function graveLeftPress()
    if gameConstant.statusSound then 
        audio.play(audioGame.buttonClick)
    end
    graveLeft.xScale = 1.5;graveLeft.yScale = 1.5;
    return true;
end 


-- forward declarations and other locals
local listenerGraveRight = function( obj )
            transition.to( graveRight, { time = timeMoveToButton, alpha=1.0,x = 2*display.contentWidth/3+20, y=display.contentHeight/3+50} )
        end

-- 'onRelease' event listener for soundLeft
local function graveRightRelease( )
	graveRight.xScale = 1;
        graveRight.yScale = 1;
        if statusGrave then statusGrave = false
        else statusGrave = true end
	return true	-- indicates successful touch
    end
-- "OnPress" event listener for backBtn
local function graveRightPress()
    if gameConstant.statusSound then 
        audio.play( audioGame.buttonClick)
    end
    graveRight.xScale = 1.5;graveRight.yScale = 1.5;
    return true;
end 

-- Move button 

local listener2 = function (obj) 
end

-- move object in frame

local function move(event)
    if statusSound then 
        soundText:setText("Sound: on ")
        audioGame.statusBackground = audio.play( audioGame.backgroundAudio, { channel=1, loops=-1, fadein=500 } )
    else 
        soundText:setText("Sound: off ")
        audio.stop( audioGame.statusBackground )
    end
    if statusGrave then graveText:setText("Show grave: on ") else graveText:setText("Show grave: off ") end
    gameConstant.statusSound = statusSound;
    
end
-----------------------------------------------------------------------------------------
-- BEGINNING OF YOUR IMPLEMENTATION
-- 
-- NOTE: Code outside of listener functions (below) will only be executed once,
--		 unless storyboard.removeScene() is called.
-- 
-----------------------------------------------------------------------------------------

-- Called when the scene's view does not exist:
function scene:createScene( event )
        print("Tao sceen Tutorial")
	local group = self.view
        --display background image
        local background = display.newImageRect( "images/menu/background.png", display.contentWidth, display.contentHeight )
	background:setReferencePoint( display.TopLeftReferencePoint )
	background.x, background.y = 0, 0
        
        -- create title logo option
        local titleLogo = display.newImage( "images/menu/lr_options.png")
        titleLogo:setReferencePoint( display.CenterReferencePoint )
        titleLogo.x = display.contentWidth/2
	titleLogo.y = 40
        
        
        -- create text sound
        soundText = display.newText("Sound: off", display.contentWidth/3+40, display.contentHeight/3-20, native.systemFont, 16 )
        soundText:setTextColor(0, 0, 0)
        graveText= display.newText("Show grave: No", display.contentWidth/3+20, display.contentHeight/3+30, native.systemFont, 16 )
        graveText:setTextColor(0, 0, 0)
        
        group:insert( background )
        group:insert( titleLogo )
        group:insert( soundText )
        group:insert( graveText )
end

-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
       local group = self.view;
       backBtn  = widget.newButton{
		labelColor = { default={255}, over={128} },
		default = "images/menu/lr_menu_back.png",
		width = 150, height = 30,
                onPress = onBackBtnPress,
		onRelease = onBackBtnRelease -- event listener function
	}
	backBtn:setReferencePoint( display.CenterReferencePoint )
	backBtn.x = display.contentWidth*0.5
	backBtn.y = 5*display.contentHeight/6+30
        
	-- all display objects must be inserted into group
	transition.from( backBtn, { time = timeMoveButton,  x=display.contentWidth/2, y=0, onComplete=listenerBackBtn } )
        
        -- create soundLeft images
        soundLeft = widget.newButton{
		default="images/menu/lr_button_left.png",
                onPress = soundLeftPress,
		onRelease = soundLeftRelease	-- event listener function
	}
        soundLeft:setReferencePoint( display.CenterReferencePoint )
	soundLeft.x = display.contentWidth/3-10
	soundLeft.y = display.contentHeight/3
        transition.from( soundLeft, { time = timeMoveButton,  x = 0, y = display.contentHeight/3,
                            onComplete=listenerSoundLeft } )
	
        -- create soundRight images
        soundRight = widget.newButton{
		default="images/menu/lr_button_right.png",
                onPress = soundRightPress,
		onRelease = soundRightRelease	-- event listener function
	}
        soundRight:setReferencePoint( display.CenterReferencePoint )
	soundRight.x = 2*display.contentWidth/3+10
	soundRight.y = display.contentHeight/3
        transition.from( soundRight, { time = timeMoveButton,  x = display.contentWidth, y = display.contentHeight/3,
                            onComplete=listenerSoundRight })
        -- create graveLeft images
       graveLeft = widget.newButton{
		default="images/menu/lr_button_left.png",
                onPress = graveLeftPress,
		onRelease = graveLeftRelease	-- event listener function
	}
        graveLeft:setReferencePoint( display.CenterReferencePoint )
	graveLeft.x = display.contentWidth/3-10
	graveLeft.y = display.contentHeight/3 + 50
        transition.from( graveLeft, { time = timeMoveButton,  x = 0, y = display.contentHeight/3+50,
                            onComplete=listenerGraveLeft } )
        -- create graveRight images
        graveRight = widget.newButton{
		default="images/menu/lr_button_right.png",
                onPress = graveRightPress,
		onRelease = graveRightRelease	-- event listener function
	}
        graveRight:setReferencePoint( display.CenterReferencePoint )
	graveRight.x = 2*display.contentWidth/3+10
	graveRight.y = display.contentHeight/3 + 50
        transition.from( graveRight, { time = timeMoveButton,  x = display.contentWidth, y = display.contentHeight/3+50,
                            onComplete=listenerGraveRight } )
        -- print("EnterScene");
        
        group:insert( soundLeft )
        group:insert( soundRight )
        group:insert( graveLeft )
        group:insert( graveRight )
        group:insert( backBtn )
        
end

-- Called when scene is about to move offscreen:
function scene:exitScene( event )
	local group = self.view
        if backBtn then 
           backBtn:removeSelf()
           backBtn = nil
          -- print ("remove backBtn")
        end
	-- INSERT code here (e.g. stop timers, remove listenets, unload sounds, etc.)
	
end

-- If scene's view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
	local group = self.view
        --backBtn:removeSelf()	-- widgets must be manually removed
        --storyboard:removeSelf("menu")
        --backBtn = nil
        print("Xoa backBtn");                                                         
end

-----------------------------------------------------------------------------------------
-- END OF YOUR IMPLEMENTATION
-----------------------------------------------------------------------------------------

-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )

-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )

-- "exitScene" event is dispatched whenever before next scene's transition begins
scene:addEventListener( "exitScene", scene )

-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )

-----------------------------------------------------------------------------------------
Runtime:addEventListener( "enterFrame", move );
return scene